
<%@ page import="gfo.Bracket; gfo.Tournament; gfo.Player" %>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <meta name="layout" content="main" />
        <g:set var="entityName" value="${message(code: 'tournament.label', default: 'Tournament')}" />
        <title><g:message code="default.list.label" args="[entityName]" /></title>
    </head>
    <body>
        <div class="body">
            <h1><g:message code="default.list.label" args="[entityName]" />
                <span class="menuButton"><g:link class="create" action="create">New Tournament</g:link></span>
            </h1>
            <g:if test="${flash.message}">
            <div class="message">${flash.message}</div>
            </g:if>
            <g:each in="${Tournament.list()}" var="tournamentInstance">
                <div class="results">
                    <g:link action="show" id="${tournamentInstance.id}"><h2>${tournamentInstance.name}</h2></g:link>
                    <h3>Round Robin:</h3>
                    <table>
                        <tbody>
                        <g:each in="${0..tournamentInstance.roster().size()}" status="i" var="playerNum">
                            <tr class="${i==0? 'value':'result'}">
                                <g:each in="${0..tournamentInstance.roster().size()}" status="j" var="oppNum">
                                    <td class="${j==0? 'value':'result'}">
                                        <g:if test="${i!=j}">
                                            <g:if test="${i==0}">
                                                <g:link controller="player" action="show" id="${tournamentInstance.roster()[j-1].id}">
                                                    ${tournamentInstance.roster()[j-1].fullName}
                                                </g:link>
                                            </g:if>
                                            <g:elseif test="${j==0}">
                                                <g:link controller="player" action="show" id="${tournamentInstance.roster()[i-1].id}">
                                                    ${tournamentInstance.roster()[i-1].fullName}
                                                </g:link>
                                            </g:elseif>
                                            <g:else test="${i!=0}">
                                                <g:link controller="match" action="show"
                                                        id="${tournamentInstance.findMatchByPlayers(Player.findById(tournamentInstance.roster()[i-1].id),
                                                                Player.findById(tournamentInstance.roster()[j-1].id))?.id}">
                                                    ${tournamentInstance.rrResult(Player.findById(tournamentInstance.roster()[i-1].id),
                                                            Player.findById(tournamentInstance.roster()[j-1].id))}
                                                </g:link>
                                            </g:else>
                                        </g:if>
                                    </td>
                                </g:each>
                                <g:if test="${i!=0}">
                                    <td class="resTotal">${tournamentInstance.rrRecord(tournamentInstance.roster()[i-1])}</td>
                                </g:if>
                            </tr>
                        </g:each>
                        </tbody>
                    </table>
                <!--This is where we use Tournament.hasBracketMatches. If the particular tournament
                                has no bracket matches, there is no need to show any of this stuff. Otherwise, I attempt
                                to setup the data in a visually interesting way--although clearly I haven't quite gotten
                                there.-->
                    <g:if test="${tournamentInstance.hasBracketMatches()}">
                        <h3>Bracket:</h3>
                        <g:each in="${Bracket.list()}" var="bracket">
                            ${bracket}<!--put tables here, not sure how to break this down-->
                        </g:each>
                    </g:if>
                </div>
            </g:each>
            <div class="list">
                <table>
                    <thead>
                        <tr>
                        
                            <g:sortableColumn property="name" title="Name" />
                        
                            <g:sortableColumn property="date" title="Date" />
                        
                        </tr>
                    </thead>
                    <tbody>
                    <g:each in="${tournamentInstanceList}" status="i" var="tournamentInstance">
                        <tr class="${(i % 2) == 0 ? 'odd' : 'even'}">
                        
                            <td><g:link action="show" id="${tournamentInstance.id}">${fieldValue(bean: tournamentInstance, field: "name")}</g:link></td>
                        
                            <td><g:formatDate type="date" dateStyle="short" date="${tournamentInstance.date}" /></td>
                        
                        </tr>
                    </g:each>
                    </tbody>
                </table>
            </div>
            <div class="paginateButtons">
                <g:paginate total="${tournamentInstanceTotal}" />
            </div>
        </div>
    </body>
</html>
